const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();

// 获取所有数据
const MAX_LIMIT = 100
exports.main = async (event, context) => {
  // const _ = db.command
  // const res = await db.collection('user').where({
  //   openId: event.openId
  // }).get()
  // // return res
  // let ans;

  const ans = await db.collection('user').doc(event.openId).set({
    // data 传入需要局部更新的数据
    data: {
      openId: event.openId,
      sex: event.sex,
      age: event.age,
    },
    success: function (res) {
      console.log(res.data)
    }
  })

  return ans
  // const countResult = await db.collection('goodList').count()
  // const total = countResult.total
  // const batchTimes = Math.ceil(total / 100)
  // const tasks = []
  // for (let i = 0; i < batchTimes; i++) {
  //   const promise = db.collection('goodList').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
  //   tasks.push(promise)
  // }
  // return (await Promise.all(tasks)).reduce((acc, cur) => {
  //   return {
  //     data: acc.data.concat(cur.data),
  //     errMsg: acc.errMsg,
  //   }
  // })
}